<template>
  <div class="login-wrap">
    <div class="header">
      <h1>游戏运营后台管理系统</h1>
    </div>
    <div class="ms-login">
      <h1>游戏运营平台登录</h1>
      <div class="formLogin">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              v-model="ruleForm.username"
              placeholder="用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="密码"
              v-model="ruleForm.password"
            ></el-input>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="login()">登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import {getTime} from "../utils/index"
import crypto from 'crypto'
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      loading: false,
    };
  },
  methods: {    
    login() {
        var md5 = crypto.createHash("md5");
        md5.update(this.ruleForm.password) //需要加密的密码
        var password = md5.digest('hex');  //password 加密完的密码
        this.loading = true;
        this.$http.post("/api/login/login", {
            username: this.ruleForm.username,
            password: password,
            user_actions:"登录",
            date:getTime(new Date())
        }).then((res) => {
          if (res.data.code == 200) {
            sessionStorage.setItem("token", res.data.token);
            sessionStorage.setItem("user", res.data.user);
            this.$message({
              message: "登录成功",
              type: "success",
            });
            this.loading = false;
            this.$router.push("/home");
          } else {
            this.$message.error("用户名和密码错误！请检查后重新登录");
            this.loading = false;
            return;
          }
      });
    },
  },
  created() {
    let that = this;
    document.onkeydown = function (e) {
      e = window.event || e;
      if (
        that.$route.path == "/login" &&
        (e.code == "Enter" || e.code == "enter")
      ) {
        //验证在登录界面和按得键是回车键enter
        that.login(); //登录函数
      }
    };
  },
};
</script>
<style scoped>
.header{
  width: 100%;
  height: 60px;
  background-color: #ffffff;
  opacity: 0.4;
}
.header h1{
  line-height: 60px;
  font-size: 30px;
  font-family:cursive;
  padding-left: 20px;
}
.login-wrap {
  background-image: url("../assets/login_bg.jpg");
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  position: relative;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 250px;
  padding: 40px;
  background: #fff;
  transform:translate(-50%, -50%);
}
.ms-login h1{
  font-size: 25px;
  text-align:center;
  font-family:Georgia, 'Times New Roman', Times, serif;
}
.formLogin{
  margin-top: 30px;
}
.login-btn {
  text-align: center;
}
.login-btn .el-button {
  width: 100%;
  height: 36px;
}
</style>